package com.lanou.impl;

import com.github.pagehelper.PageHelper;
import com.lanou.mapper.prePayment;
import com.lanou.pojo.ConfigHouseEntity;
import com.lanou.pojo.PaymentMethod;
import com.lanou.pojo.PaymentPreAccountEntity;
import com.lanou.pojo.PaymentPreEntity;
import com.lanou.service.prePaymentService;
import com.lanou.staticResource.FilePath;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Service
public class prePaymentServiceImpl implements prePaymentService {
    @Resource
    private prePayment Payment;

    @Resource
    private com.lanou.mapper.securityDeposit securityDeposit;
    @Override
    public List<PaymentPreEntity> selectPaymentPreList(String pageNum, String pageSize, String resourceName, String feeUser, String type, String feeItemName, String payType) {
        //处理分页查询
        if(pageNum == null || "".equals(pageNum)){
            pageNum="1";
        }
        if(pageSize == null || "".equals(pageSize)){
            pageSize="5";
        }
        PageHelper.startPage(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        
        List<PaymentPreEntity> paymentPreEntities = Payment.selectPaymentPreList(resourceName, feeUser, type, feeItemName, payType);
        List<PaymentMethod> paymentMethods = securityDeposit.selectPayMethod();
        for (PaymentPreEntity code : paymentPreEntities) {
            for(PaymentMethod name:paymentMethods)
                if (code.getPayType().equals(name.getCode())) {
                    code.setPayType(name.getName());
                }
        }
        return paymentPreEntities;
    }
    @Override
    public void addPrePayment(String resourceName, String feeUser, String feeItemName, String payType, String amt, Date operateTime) {
        String  id=controllerServiceImpl.generateRandomNumber(20);
        String resourceId=controllerServiceImpl.generateRandomNumber(20);
        String feeItemId=controllerServiceImpl.generateRandomNumber(20);
        String lastModifyUserId=controllerServiceImpl.generateRandomNumber(20);



        PaymentPreAccountEntity paymentPreAccountEntity =Payment.selectPaymentPreAccount(resourceName);

        System.out.println("==================================");
        if(feeItemName == null || feeItemName.isEmpty()) {
            feeItemName = "不指定收费项";
        }

        if(paymentPreAccountEntity == null) {
                Payment.addPrePaymentAccount(id, resourceId, feeItemId, resourceName, amt);
        } else {
            if (paymentPreAccountEntity.getFeeItemId()==null){
                Payment.addPrePaymentAccount(id, resourceId, feeItemId, resourceName, amt);
                System.out.println("rtyfghujklctyuijkolptyuihjkoltyui");
            }

            System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
            String money = String.valueOf(Double.parseDouble(paymentPreAccountEntity.getAmt()) + Double.parseDouble(amt));
            Payment.updatePrePaymentAccount(money, lastModifyUserId, paymentPreAccountEntity.getFeeItemId());
            System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
        }
        Payment.addPrePayment(id,resourceId,feeItemId,resourceName,feeUser,feeItemName,payType,amt,operateTime);
    }

    @Override
    public PaymentPreAccountEntity selectPaymentPreAccount(String resourceName) {
        PaymentPreAccountEntity paymentPreAccountEntities =  Payment.selectPaymentPreAccount(resourceName);

        return paymentPreAccountEntities;
    }

    @Override
    public List<PaymentPreAccountEntity> selectPaymentPreAccountMessage(String resourceName) {
        List<PaymentPreAccountEntity> paymentPreAccountEntities = Payment.selectPaymentPreAccountMessage(resourceName);
        List<String> strings = Payment.selectPayItem(resourceName);
        System.out.println(strings);
        System.out.println(paymentPreAccountEntities);
//        System.out.println("======================================");
//        if (paymentPreAccountEntities != null) {
//            for (PaymentPreAccountEntity feeItemId : paymentPreAccountEntities) {
//                if (feeItemId.getFeeItemId() == null || "".equals(feeItemId.getFeeItemId())) {
//                    feeItemId.setFeeItemId("不指定收费项");
//                } else {
//                    for (String itemName : strings) {
//                        if (feeItemId.getFeeItemId().equals(itemName)) {
//                            feeItemId.setFeeItemId(itemName);
//                            break; // 可以添加一个break语句，避免继续循环
//                        }
//                    }
//                }
//            }
//        }
        return paymentPreAccountEntities;
    }

    @Override
    public void updatePrePaymentAccount(String amt, String lastModifyUserId, String feeItemId) {
        Payment.updatePrePaymentAccount(amt,lastModifyUserId,feeItemId);
    }

    @Override
    public void updatePayMessage(String resourceName,String payMethod, Date time, String backmoney, String itemId) {
        PaymentPreAccountEntity paymentPreAccountEntities =  Payment.selectPaymentPreAccount(resourceName);
        String lastModifyUserId=controllerServiceImpl.generateRandomNumber(20);
        if (backmoney!=null || "".equals(backmoney)){
            backmoney = String.valueOf(Double.parseDouble(paymentPreAccountEntities.getAmt()) -Double.parseDouble(backmoney));
        }
        Payment.updatePrePaymentAccount(backmoney,lastModifyUserId,itemId);
        List<String> strings = Payment.selectPayItem(resourceName);
        for (String s:strings){
            Payment.updatePre(lastModifyUserId,s);
        }

    }

    @Override
    public List<ConfigHouseEntity> selectShopMessageList(String pageNum , String pageSize) {
        //处理分页查询
        if(pageNum == null || "".equals(pageNum)){
            pageNum="1";
        }
        if(pageSize == null || "".equals(pageSize)){
            pageSize="5";
        }
        PageHelper.startPage(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        List<ConfigHouseEntity> configHouseEntities = Payment.selectShopMessageList();
        return configHouseEntities;
    }

    @Override
    public String uploadFile(MultipartFile multipartFile) throws IOException {
        String fileName = multipartFile.getOriginalFilename();
        XSSFWorkbook sheets = new XSSFWorkbook(FilePath.FILE_PATH + "商铺导入模板.xlsx");
        XSSFSheet sheetAt = sheets.getSheetAt(0);
        int lastRowNum = sheetAt.getLastRowNum(); //获取表格中最后有内容的行的索引，从0开始
        Map<Integer, List<String>> map =  new HashMap<>();
        for (int i=2;i<=lastRowNum;i++){
            List<String> list=new ArrayList<>();
            XSSFRow row = sheetAt.getRow(i);
            for (Cell cell:row) {
                String stringCellValue = cell.getStringCellValue();
                list.add(stringCellValue);
                System.out.println(stringCellValue);
            }
            map.put(i-2,list);
        }
        for (Map.Entry<Integer, List<String>> entry : map.entrySet()) {
            List<String> value = entry.getValue();
            Payment.insertConfihHouse(value.get(0), value.get(1), value.get(2), value.get(3), value.get(4), value.get(5), value.get(6));
        }

        return null;
    }


}
